<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/public/header.jsp"%>
<%
    String callback = request.getParameter("callback");
	String isShow = request.getParameter("isShow") == null ? "" : request.getParameter("isShow");
	String xm_id = request.getParameter("xm_id") == null ? "" : request.getParameter("xm_id");
	String xm_code = request.getParameter("xm_code") == null ? "" : request.getParameter("xm_code");
	String xm_name = request.getParameter("xm_name") == null ? "" : request.getParameter("xm_name");
	String customer_id = request.getParameter("customer_id") == null ? "" : request.getParameter("customer_id");
	String customer = request.getParameter("customer") == null ? "" : request.getParameter("customer");
	String id = request.getParameter("id") == null ? "" : request.getParameter("id");
	Map<String, Object> sessionMap = (Map<String, Object>) session.getAttribute("LOGIN_USER");
	String person_id = String.valueOf(sessionMap.get("id"));
	String person = String.valueOf(sessionMap.get("truename"));
	boolean isAdd = "".equals(id);
    int flag = 0;
    if(!isAdd){
    	flag = 1;
    }
%>
<html>
	<head>
		<title><%=isAdd?"新建项目支付":"编辑项目支付" %></title>
		<script type="text/javascript" src="<%=contextPath %>/public/js/extend.js"></script>
		<style>
			.td1{
				padding-top:10px;
				padding-bottom:5px;
				font-size: 12px;
				background-color: #c0ebd7;
				width:20%;
			}
			.td2{
				padding:5px;
			}
		</style>
	<script type="text/javascript">
		var lastIndex;
	 var loadUrl='<%=contextPath %>/springMVC/xm/getXM?xm_id=<%=xm_id%>';
	 var submitUrl = '<%=contextPath %>/springMVC/xmFinance/addXMEndPayMain';
	 var url = '<%=contextPath %>/springMVC/xmFinance/getXMPurchaseBalanceByXMId?xm_id=<%=xm_id%>';
		$(function(){
			$('#pay_value').numberbox('disable',true);
		   $.ajax({
				type:'POST',
				url:'<%=contextPath %>/springMVC/xmFinance/hasXMEndPayMain?pay_type=2&xm_id=<%=xm_id%>',
				async:false,
				success:function(rtjson){
					if(rtjson != '0'){
						url = '<%=contextPath %>/springMVC/xmFinance/listXMEndPayMain?pay_type=2&xm_id=<%=xm_id%>';
						submitUrl = '<%=contextPath %>/springMVC/xmFinance/updateXMEndPayMain';
						loadUrl='<%=contextPath %>/springMVC/xmFinance/getXmEndPayMain?pay_type=2&xm_id=<%=xm_id%>';
						$('#submitValue').html("重新保存");
					}
				}
			});
			$('#ff').form({
				onLoadSuccess:function(data){
				     $.ajax({
					    	type:'POST',
					    	url:url,
					    	success:function(rtJson){
							    if(rtJson != null && rtJson.length>0){
							    	rtJson = eval(rtJson);
						    		$('#person').val(rtJson[0].creator);
							    }
					    	}
					    });
				}
			});
		   $('#ff').form('load',loadUrl);
			  
	       $('#table').datagrid({
	    	    url:url,
				rownumbers:true,
				fitColumns:true,
				nowrap:false,
				frozenColumns:[[
				    {title:'工单支付id',field:'bus_id',hidden:true}
				]],
				columns:[[
					{title:'供应商ID',field:'supplier_id',hidden:true},
					{title:'供应商',field:'company',width:150,align:'center'},
					{title:'工单id',field:'gd_id',hidden:true},
					{title:'工单编号',field:'gd_code',width:250,align:'center'},
					{title:'工单名称',field:'gd_name',width:150,align:'center'},
					{title:'应付款',field:'actual_pay_total',width:200,align:'center',formatter:function(value,rowData,index){
						return (parseFloat(rowData.finance_out)+parseFloat(rowData.already_pay)).toFixed(2);
					}},
					{title:'已付款',field:'already_pay',width:200,align:'center',formatter:function(value,rowData,index){
						if(isNaN(value)){
							rowData.already_pay=0;
						}
						return rowData.already_pay.toFixed(2);
					}},
					{title:'应付款',hidden:true,field:'finance_out',width:200,align:'center',formatter:function(value,rowData,index){
						return rowData.finance_out.toFixed(2);
					}},
					{title:'结算支付',field:'end_pay',width:100,align:'center',formatter:function(value,rowData,index){
						if(isNaN(value)){
							rowData.end_pay= rowData.finance_out;
						}
						return rowData.end_pay.toFixed(2);
					}},
					{title:'备注',field:'remark',width:200,align:'center',editor:{
						type:'textarea'
					}}
				]],
				onBeforeLoad:function(param){
					
					$(this).datagrid('rejectChanges');
					
				},
				onAfterEdit:function(rowIndex, rowData, changes){
				calculaMoney();
				},
				onClickCell:function(rowIndex,field,value){
					if (lastIndex != rowIndex){
						$('#table').datagrid('endEdit', lastIndex);
						$('#table').datagrid('beginEdit', rowIndex);
						var ed = $('#table').datagrid('getEditor', {index:rowIndex,field:field});
						if(ed == null){
							ed = $('#table').datagrid('getEditor', {index:rowIndex,field:'remark'});
						}
						$(ed.target).select();
					}
					lastIndex = rowIndex;
				},
				onLoadSuccess:function(rtJson){
					if(rtJson.rtState== '1')
					{
						
						$.messager.alert('提示',rtJson.rtMsrg);
					}
					calculaMoney();
				},
				toolbar:[
					{
					id:'save',
					text:'保存修改',
					disabled:false,
					iconCls:'icon-save',
					handler:function(){
						$('#table').datagrid('acceptChanges');
						lastIndex =-1;
						calculaMoney();	
	
					}
					}
			   ]
			});
			
		});
		
		function calculaMoney(){
				var inMoneys=0;
				var jsondata=$("#table").datagrid("getData");
				for(var i=0;i<jsondata.total;i++){
					
				inMoneys+=parseFloat(jsondata.rows[i].end_pay);	
				
				}
				$('#pay_value').numberbox('setValue', inMoneys);
		}
		function addXMPaymentSupplyBalance(obj){
			//自动提交数据
			$('#table').datagrid('acceptChanges');
			lastIndex =-1;
			//自动提交数据
			var xm_id =jQuery("#xm_id").val();	
			var xm_name=jQuery("#xm_name").val();	
			var xm_code=jQuery("#xm_code").val();
			var pay_value=jQuery("#pay_value").val();
			var remark=encodeURIComponent(jQuery('#remark').val());
			var customer=encodeURIComponent(jQuery('#customer').val());
			var admin_dept=jQuery("#admin_dept").val();
			var admin_dept_id=jQuery("#admin_dept_id").val();
			if(!checkNullOREmpty(xm_id,'项目为必填项')){return;}
			
			var gds="";
			var jsondata=$("#table").datagrid("getData");
			for(var i=0;i<jsondata.total;i++){
				if(gds!=""){
					gds+=";";
				}
				gds+=jsonToStr(jsondata.rows[i]);
				var index = $("#table").datagrid("getRowIndex",jsondata.rows[i])+1;
				if(jsondata.rows[i].end_pay == null  || parseFloat(jsondata.rows[i].end_pay) <0){
					$.messager.alert('提示','第'+index+'行结算金额有误！');
					return ;
				}else if(parseFloat(jsondata.rows[i].end_pay)>parseFloat(jsondata.rows[i].finance_out)){
					$.messager.alert('提示','第'+index+'行支付金额不能大于应付金额！');
					return ;
				}
			}
			if(gds == ""){
				$(obj).hide();
				$.messager.alert('提示','无提交数据！');
				return;
			}
			var param = "pay_type=2&remark="+remark+"&customer="+customer+"&admin_dept="+admin_dept+"&admin_dept_id="+admin_dept_id
			+"&pay_value="+pay_value
			+"&xm_id="+xm_id+"&xm_code="+xm_code+"&xm_name="+xm_name+"&gds="+gds;
			$(obj).hide();
			$.ajax({
				type:'POST',
				url:submitUrl,
				data:param,
				dataType:'json',
				success:function(rtJson){
					if(rtJson.rtState == '0'){
						$.messager.alert("提示",rtJson.rtMsrg);
						//submitUrl = '<%=contextPath %>/springMVC/xmFinance/updateXMEndPayMain';
					}
					else{
						$.messager.alert("提示",rtJson.rtMsrg);
					}
					//$(obj).show();
				}
			});
		}
	</script>
	</head>
	<body>
		<div style="width: 97%;padding-left:10px">
		<form id="ff">
		<input  type="hidden" id="pay_to_id" name="pay_to_id" >
		<input  type="hidden" id="pay_to_name" name="pay_to_name" >
			<table  style="border-collapse: collapse;" width="100%" border="1px" bordercolor="#0eb83a">
				<tr>
					<td align="right" class="td1" nowrap>项目编号：</td>
					<td class="td2" nowrap>
						<input  id="xm_code" name="xm_code" readOnly value="<%=xm_code%>">
						<input type="hidden" id="xm_id" name="xm_id" readOnly value="<%=xm_id%>">
					</td>
					<td align="right" class="td1" nowrap>项目名称：</td>
					<td class="td2" nowrap>
						<input  id="xm_name" name="xm_name" readOnly value="<%=xm_name%>">
					</td>
				</tr>
				<tr>
					<td align="right" class="td1" nowrap>项目部门：</td>
					<td class="td2" nowrap>
						<input  id="admin_dept" name="admin_dept" readOnly value="">
						<input  type="hidden" id="admin_dept_id" name="admin_dept_id" readOnly value="">
					</td>
					<td align="right" class="td1" nowrap>经办人：</td>
					<td class="td2" nowrap>
					  <%if(!"1".equals(isShow)){ %>
						<input  id="person" name="person" readOnly value="<%=person%>">
					  <%} else{%>
					  	<input  id="person" name="person" readOnly >
					  <%}%>
					</td>
				</tr>
				<tr>
					<td align="right" class="td1" nowrap>对应客户：</td>
					<td class="td2" nowrap colspan="3">
						<input  id="customer" name="customer" readOnly value="">
						<input  type="hidden" id="customer_id" name="customer_id" readOnly value="">
					</td>
				</tr>
				<tr>	
			        <td align="right" class="td1" nowrap>付款总额：</td>
					<td class="td2" nowrap colspan="3">
						<input  id="pay_value" name="pay_value" class="easyui-numberbox" data-options="min:0,precision:2" value="0" >(自动计算)
					</td>
				</tr>				
				<tr>
					<td align="right" class="td1" nowrap>摘要：</td>
					<td class="td2" nowrap colspan='3'>
						<textarea  id="remark" name="remark" rows="6" style="width: 100%"></textarea>
					</td>
				</tr>
				<tr>
					<td align="right" class="td1" nowrap></td>
					<td class="td2" nowrap colspan='3'>
						<div align="left">
						   <%if(!"1".equals(isShow)){ %>
							<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-save',plain:false" onclick="addXMPaymentSupplyBalance(this);"><span id="submitValue">提交保存</span></a>
							<%} %>
						</div>
					</td>
				</tr>
				<tr>
					<td colspan="4"  nowrap>
						<div id="table"></div>
					</td>
				</tr>
			</table>
			</form>
		</div>
		
	</body>
</html>